package com.chiyou.common.utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

/**
 * 属性值获取工具类
 * @author 陈嘉瑛
 * @version 2016-01-22
 * 
 */
public class PropertyUtil {

	private static final Map<String, TreeMap<String, String>> proMap = new TreeMap<String, TreeMap<String, String>>();
	
	private static final String PRO_FILENAME = "app.properties";
	
	private static final String FLORD = "properties/";
	
	@SuppressWarnings("rawtypes")
	private static void init(String fileName){
		Properties p = new Properties();
		TreeMap<String, String> map = new TreeMap<String, String>();
		try {
			p.load(PropertyUtil.class.getClassLoader().getResourceAsStream(FLORD+fileName));
			for (Iterator it = p.keySet().iterator(); it.hasNext();) {
				String key = (String) it.next();
				map.put(key, p.getProperty(key));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		proMap.put(fileName, map);
	}

	/**
	 * 获取指定properties文件的指定key的value值
	 * @param proFileName
	 * @param proName
	 * @return
	 */
	public static String getValue(String proFileName,String proName) {
		if(proMap.get(proFileName) == null) {
			init(proFileName);
		}
		return proMap.get(proFileName).get(proName);
	}
	
	
	public static String getValue(String key){
		return getValue(PRO_FILENAME,key);
	}

	public static void main(String[] args){
		System.out.println(PropertyUtil.getValue("conf.properties", "soho.base.url").trim());
	}
}

